package com.chwn.curiousdiary.ui.story.presenter;

import com.chwn.curiousdiary.DiaryApplication;
import com.chwn.curiousdiary.api.IHomeApi;
import com.chwn.curiousdiary.api.NetWork;
import com.chwn.curiousdiary.bean.StoryCountBean;
import com.chwn.curiousdiary.bean.StoryListBean;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.ui.story.contract.StoryListContract;
import com.chwn.harulib.mvp.rx.HttpResult;
import com.chwn.harulib.mvp.rx.HttpResultException;
import com.chwn.harulib.mvp.rx.RxSchedulers;

import java.util.List;

import rx.Observer;

/**
 * Created by Myshikin on 2017/4/11.
 */

public class StoryListPresenter extends StoryListContract.Presenter {

    private Observer<HttpResult<List<StoryListBean>>> homeListObserver = new Observer<HttpResult<List<StoryListBean>>>() {
        @Override
        public void onCompleted() {
            mView.onRequestSuccess();
        }

        @Override
        public void onError(Throwable e) {
            mView.onRequestError(e);
        }

        @Override
        public void onNext(HttpResult<List<StoryListBean>> httpResult) {
            if (httpResult.getResult() == 1) {
                List<StoryListBean> data = httpResult.getResdata();
                mView.showStoryList(data);
            } else {
                mView.onRequestError(new HttpResultException(2, "数据为空"));
            }
        }
    };

    private Observer<HttpResult<List<StoryListBean>>> newListObserver = new Observer<HttpResult<List<StoryListBean>>>() {
        @Override
        public void onCompleted() {
            mView.onRequestSuccess();
        }

        @Override
        public void onError(Throwable e) {
            mView.onRequestError(e);
        }

        @Override
        public void onNext(HttpResult<List<StoryListBean>> httpResult) {
            if (httpResult.getResult() == 1) {
                List<StoryListBean> data = httpResult.getResdata();
                for (StoryListBean bean : data) {
                    bean.setsParts(1);
                }
                mView.showStoryList(data);
            } else {
                mView.onRequestError(new HttpResultException(2, "数据为空"));
            }
        }
    };

    private Observer<HttpResult<StoryCountBean>> storyTypeCountPresenter = new Observer<HttpResult<StoryCountBean>>() {
        @Override
        public void onCompleted() {
            mView.onRequestSuccess();
        }

        @Override
        public void onError(Throwable e) {
            mView.onRequestError(e);
        }

        @Override
        public void onNext(HttpResult<StoryCountBean> httpResult) {
            if (httpResult.getResult() == 1) {
                StoryCountBean data = httpResult.getResdata();
                mView.showStoryTypeCount(data.getAllStoryCount());
            }
        }
    };

    @Override
    public void getStoryList(String storyType, int page) {
        String userToken = DiaryApplication.mInstance.getUserToken();
        if ("-1".equals(storyType)) {
            NetWork.getInstance()
                    .getApiService(IHomeApi.class, true)
                    .getNewestStoryList(Constant.PAGE_COUNT ,userToken, page, Constant.OSTYPE)
                    .compose(RxSchedulers.<HttpResult<List<StoryListBean>>>io_main())
                    .subscribe(newListObserver);
        } else if ("-2".equals(storyType)) {
            NetWork.getInstance()
                    .getApiService(IHomeApi.class ,true)
                    .getHomeList(Constant.PAGE_COUNT,userToken , page, Constant.OSTYPE)
                    .compose(RxSchedulers.<HttpResult<List<StoryListBean>>>io_main())
                    .subscribe(newListObserver);

        } else {
            NetWork.getInstance()
                    .getApiService(IHomeApi.class, true)
                    .getStoryTypeList(storyType, userToken ,Constant.PAGE_COUNT, page, Constant.OSTYPE)
                    .compose(RxSchedulers.<HttpResult<List<StoryListBean>>>io_main())
                    .subscribe(homeListObserver);
        }
    }

    @Override
    public void getStoryTypeCount(String storyPackageId) {
        NetWork.getInstance()
                .getApiService(IHomeApi.class)
                .getStoryTypeCount(storyPackageId, Constant.OSTYPE)
                .compose(RxSchedulers.<HttpResult<StoryCountBean>>io_main())
                .subscribe(storyTypeCountPresenter);
    }
}
